import { defineStore } from 'pinia'
import type { FlowData, ModelInfo, FormProperties, FlowNodeConfig, DesignFormComponent, DesignFlowNode, Task } from './model'
import http from '@/tools/http'

export const useFlowStore = defineStore('flow', {
    state: (): FlowData => ({
        modelInfo: {
            modelId: "",
            modelVersion: 1,
            modelName: "",
            modelGroup: "",
            modelStatus: "",
            sort: 999
        },
        formProperties: {
            xGap: 12,
            labelPlacement: "0",
            labelWidth: 100,
            requireMarkPlacement: "right"
        },
        formComponentList: [],
        nextNodeId: 3,
        zoomConfig: 100,
        flowNodeList: [],
        mode: "0",
        currentStep: "modelInfo",
        instanceInfo: undefined,
        taskList: [],
        triggerList: []
    }),
    getters: {
        getLabelPlacement(state): 'left' | 'top' {
            if (state.formProperties.labelPlacement === "0") {
                return "top"
            }
            return "left"

        },
        getLabelAlign(state): 'left' | 'center' | 'right' {
            if (state.formProperties.labelPlacement === "2") {
                return "right"
            }
            return "left"
        },
        flowNodeTree(state): DesignFlowNode[] {
            return getChildren(state.flowNodeList, 0)
        },
        approvalNodeList(state) {
            const list = []
            for (let i = 0; i < state.flowNodeList.length; i++) {
                if (state.flowNodeList[i].nodeType === "0" || state.flowNodeList[i].nodeType === "1") {
                    list.push({
                        nodeId: state.flowNodeList[i].nodeId,
                        nodeName: state.flowNodeList[i].nodeName
                    })
                }
            }
            return list
        },
        getTriggerDesc(state) {
            return (springBeanName: string): string => {
                for (let i = 0; i < state.triggerList.length; i++) {
                    if (state.triggerList[i].springBeanName === springBeanName) {
                        return state.triggerList[i].desc
                    }
                }
                return springBeanName
            }
        }
    },
    actions: {
        addNode(nodeId: number, nodeType: string) {
            if (!["0"].includes(this.mode)) {
                return
            }
            const newNodeId = this.nextNodeId++

            if (nodeType !== "3" && nodeType !== "5") {
                for (let i = 0; i < this.flowNodeList.length; i++) {
                    if (this.flowNodeList[i].parentNodeId === nodeId) {
                        if (this.flowNodeList[i].nodeType !== "3" && this.flowNodeList[i].nodeType !== "5") {
                            this.flowNodeList[i].parentNodeId = newNodeId
                        }
                    }
                }
            }

            const newNode: DesignFlowNode = {
                nodeId: newNodeId,
                nodeType: nodeType,
                nodeName: getNodeName(nodeType),
                parentNodeId: nodeId,
                config: {},
                isEdit: false,
                isNameEdit: false
            }
            if (nodeType === "1" || nodeType === "6") {
                if (nodeType === "1") {
                    newNode.nodeName = newNode.nodeName + (getNodeCount(this.flowNodeList, "1") + 1)
                    newNode.config = {
                        // 审批人类型
                        approvalType: "0",
                        // 审批人列表
                        approvalList: [],
                        approvalEnd: -1,
                        // 多人审批通过方式
                        multiApprovalType: "0",
                        // 审批不通过类型
                        approvalRefusedType: "0"
                    }
                }
                if (nodeType === "6") {
                    newNode.nodeName = newNode.nodeName + (getNodeCount(this.flowNodeList, "6") + 1)
                }
                this.flowNodeList.push(newNode)
            }
            if (nodeType === "3" || nodeType === "5") {
                const branchNodeList = getBranchNodeList(this.flowNodeList, nodeId)
                newNode.nodeName = newNode.nodeName + (branchNodeList.length + 1)
                this.flowNodeList.push(newNode)
            }
            if (nodeType === "2") {
                this.flowNodeList.push(newNode)
                this.flowNodeList.push({
                    nodeId: this.nextNodeId++,
                    nodeType: "3",
                    nodeName: getNodeName("3") + 1,
                    parentNodeId: newNodeId,
                    config: {},
                    isEdit: false,
                    isNameEdit: false
                })
                this.flowNodeList.push({
                    nodeId: this.nextNodeId++,
                    nodeType: "3",
                    nodeName: getNodeName("3") + 2,
                    parentNodeId: newNodeId,
                    config: {},
                    isEdit: false,
                    isNameEdit: false
                })
            }
            if (nodeType === "4") {
                this.flowNodeList.push(newNode)
                this.flowNodeList.push({
                    nodeId: this.nextNodeId++,
                    nodeType: "5",
                    nodeName: getNodeName("5") + 1,
                    parentNodeId: newNodeId,
                    config: {},
                    isEdit: false,
                    isNameEdit: false
                })
                this.flowNodeList.push({
                    nodeId: this.nextNodeId++,
                    nodeType: "5",
                    nodeName: getNodeName("5") + 2,
                    parentNodeId: newNodeId,
                    config: {},
                    isEdit: false,
                    isNameEdit: false
                })
            }
        },
        deleteNode(nodeId: number) {
            if (["1", "2", "3", "4"].includes(this.mode)) {
                return
            }
            let i = 0
            for (; i < this.flowNodeList.length; i++) {
                if (this.flowNodeList[i].nodeId === nodeId) {
                    break
                }
            }
            if (this.flowNodeList[i].nodeType === "1" || this.flowNodeList[i].nodeType === "6") {
                for (let j = 0; j < this.flowNodeList.length; j++) {
                    if (this.flowNodeList[j].parentNodeId === this.flowNodeList[i].nodeId) {
                        this.flowNodeList[j].parentNodeId = this.flowNodeList[i].parentNodeId
                    }
                }
                this.flowNodeList.splice(i, 1)
            }
            if (this.flowNodeList[i].nodeType === "3" || this.flowNodeList[i].nodeType === "5") {
                // 找到父节点
                let j = 0
                for (; j < this.flowNodeList.length; j++) {
                    if (this.flowNodeList[j].nodeId === this.flowNodeList[i].parentNodeId) {
                        break
                    }
                }
                // 看看有几个分支，2个分支：全删
                const branchNodeList = getBranchNodeList(this.flowNodeList, this.flowNodeList[j].nodeId)
                if (branchNodeList.length > 2) {
                    this.flowNodeList[i].parentNodeId = -1
                } else {
                    const childNode = getChildren(this.flowNodeList, this.flowNodeList[j].nodeId)[0]
                    if (childNode) {
                        let k = 0;
                        for (; k < this.flowNodeList.length; k++) {
                            if (this.flowNodeList[k].nodeId === childNode.nodeId) {
                                break
                            }
                        }
                        this.flowNodeList[k].parentNodeId = this.flowNodeList[j].parentNodeId
                    }
                    this.flowNodeList[j].parentNodeId = -1
                }
                // 移除所有找不到父节点的节点
                this.cleanUnReachNode()
            }
        },
        cleanUnReachNode() {
            let continueCleanflag = true
            while (continueCleanflag) {
                continueCleanflag = false
                let i = 0
                for (; i < this.flowNodeList.length; i++) {
                    if (this.flowNodeList[i].nodeType === "0") {
                        continue
                    }
                    let hasParent = false
                    for (let j = 0; j < this.flowNodeList.length; j++) {
                        if (this.flowNodeList[i].parentNodeId === this.flowNodeList[j].nodeId) {
                            hasParent = true
                            break
                        }
                    }
                    if (hasParent === false) {
                        continueCleanflag = true
                        break
                    }
                }
                this.flowNodeList.splice(i, 1)
            }
        },
        openNode(nodeId: number) {
            if (["2", "3", "4"].includes(this.mode)) {
                return
            }
            for (let i = 0; i < this.flowNodeList.length; i++) {
                if (this.flowNodeList[i].nodeId === nodeId) {
                    this.approvalListUpdate(nodeId, this.flowNodeList[i].config.approvalList)
                    this.flowNodeList[i].isEdit = true
                    break
                }
            }
        },
        approvalListUpdate(nodeId: number, value?: string[]) {
            let i = 0
            for (; i < this.flowNodeList.length; i++) {
                if (this.flowNodeList[i].nodeId === nodeId) {
                    break
                }
            }
            this.flowNodeList[i].config.approvalList = value
            let request: string[] = []
            if (value) {
                request = value
            }

            if (this.flowNodeList[i].config.approvalType === "0") {
                http.post("/id/user/list", request).then(({ data }) => {
                    if (data.code === "0") {
                        this.flowNodeList[i].config.approvalUserList = []
                        for (let j = 0; j < data.data.length; j++) {
                            this.flowNodeList[i].config.approvalUserList?.push(data.data[j])
                        }
                    }
                })
            }

            if (this.flowNodeList[i].config.approvalType === "1") {
                http.post("/id/role/query", request).then(({ data }) => {
                    if (data.code === "0") {
                        this.flowNodeList[i].config.approvalRoleList = []
                        for (let j = 0; j < data.data.length; j++) {
                            this.flowNodeList[i].config.approvalRoleList?.push(data.data[j])
                        }
                    }
                })
            }

            if (this.flowNodeList[i].config.approvalType === "2") {
                http.post("/id/dept/list").then(({ data }) => {
                    if (data.code === "0") {
                        this.flowNodeList[i].config.approvalOrgList = []
                        for (let j = 0; j < data.data.length; j++) {
                            if (request.includes(data.data[j].orgCode)) {
                                this.flowNodeList[i].config.approvalOrgList?.push(data.data[j])
                            }
                        }
                    }
                })
            }
        },
        reset() {
            this.formProperties = {
                xGap: 12,
                labelPlacement: "0",
                labelWidth: 100,
                requireMarkPlacement: "right"
            }
            this.formComponentList.length = 0
            this.nextNodeId = 3
            this.flowNodeList.length = 0
            this.flowNodeList.push({
                nodeId: 1,
                nodeType: "0",
                nodeName: "流程开始",
                parentNodeId: 0,
                config: {},
                isEdit: false,
                isNameEdit: false
            })
            this.flowNodeList.push({
                nodeId: 2,
                nodeType: "9",
                nodeName: "流程结束",
                parentNodeId: 1,
                config: {},
                isEdit: false,
                isNameEdit: false
            })
            this.zoomConfig = 100
            this.mode = "0"
            this.currentStep = "modelInfo"
            this.instanceInfo = undefined
            this.taskList.length = 0
            this.triggerList.length = 0
        },
        initWorkflowModel(modelId: string, modelVersion: number, mode: string, instanceId?: string, taskId?: string) {
            this.reset()
            this.mode = mode
            if (this.mode === "0" || this.mode === "1") {
                this.currentStep = "modelInfo"
            }
            if (["2", "3", "4", "5"].includes(this.mode)) {
                this.currentStep = "flowform"
            }
            http.post("/workflow/ru/trigger/list").then(({ data }) => {
                if (data.code === "0") {
                    for (let key in data.data) {
                        this.triggerList.push({
                            springBeanName: key,
                            desc: data.data[key],
                            label: `${data.data[key]}:${key}`
                        })
                    }
                }
            })
            const request = {
                modelId: modelId,
                modelVersion: modelVersion
            }
            http.post("/workflow/re/model", request).then(({ data }) => {
                if (data.code === "0") {
                    // 模型基础信息
                    this.modelInfo.modelId = data.data.model.modelId
                    this.modelInfo.modelVersion = data.data.model.modelVersion
                    this.modelInfo.modelName = data.data.model.modelName
                    this.modelInfo.modelGroup = data.data.model.modelGroup
                    this.modelInfo.modelStatus = data.data.model.modelStatus
                    this.modelInfo.sort = data.data.model.sort
                    // 表单信息
                    if (data.data.form) {
                        this.formProperties.xGap = data.data.form.xGap
                        this.formProperties.labelPlacement = data.data.form.labelPlacement
                        this.formProperties.labelWidth == data.data.form.labelWidth
                        this.formProperties.requireMarkPlacement = data.data.form.requireMarkPlacement
                    }
                    // 表单项信息
                    if (data.data.formComponentList) {
                        for (let i = 0; i < data.data.formComponentList.length; i++) {
                            const component: any = {
                                componentId: data.data.formComponentList[i].componentCode,
                                componentCode: data.data.formComponentList[i].componentCode,
                                isDragging: false,
                                isSelected: false
                            }
                            for (let j = 0; j < data.data.propertyList.length; j++) {
                                if (data.data.formComponentList[i].componentCode === data.data.propertyList[j].objectId) {
                                    if (["componentOptions"].includes(data.data.propertyList[j].propertyKey)) {
                                        component[data.data.propertyList[j].propertyKey] = JSON.parse(data.data.propertyList[j].propertyValue)
                                        continue
                                    }
                                    if (["componentSpan"].includes(data.data.propertyList[j].propertyKey)) {
                                        component[data.data.propertyList[j].propertyKey] = parseInt(data.data.propertyList[j].propertyValue)
                                        continue
                                    }
                                    component[data.data.propertyList[j].propertyKey] = data.data.propertyList[j].propertyValue
                                }
                            }
                            this.formComponentList.push(component)
                        }
                    }
                    // 节点信息
                    if (data.data.nodeList && data.data.nodeList.length !== 0) {
                        let maxId = 3
                        this.flowNodeList.length = 0
                        for (let i = 0; i < data.data.nodeList.length; i++) {
                            maxId = maxId > data.data.nodeList[i].nodeId ? maxId : data.data.nodeList[i].nodeId + 1
                            const flowNode: any = {
                                nodeId: data.data.nodeList[i].nodeId,
                                nodeName: data.data.nodeList[i].nodeName,
                                nodeType: data.data.nodeList[i].nodeType,
                                parentNodeId: data.data.nodeList[i].parentNodeId,
                                config: {},
                                isEdit: false,
                                isNameEdit: false
                            }
                            for (let j = 0; j < data.data.propertyList.length; j++) {
                                if ('' + data.data.nodeList[i].nodeId === data.data.propertyList[j].objectId) {
                                    if (["approvalList"].includes(data.data.propertyList[j].propertyKey)) {
                                        flowNode.config[data.data.propertyList[j].propertyKey] = JSON.parse(data.data.propertyList[j].propertyValue)
                                        continue
                                    }
                                    if (["approvalRefusedToNodeId"].includes(data.data.propertyList[j].propertyKey)) {
                                        flowNode.config[data.data.propertyList[j].propertyKey] = parseInt(data.data.propertyList[j].propertyValue)
                                        continue
                                    }
                                    flowNode.config[data.data.propertyList[j].propertyKey] = data.data.propertyList[j].propertyValue
                                }
                            }
                            this.flowNodeList.push(flowNode)
                            if ("1" === flowNode.nodeType) {
                                this.approvalListUpdate(flowNode.nodeId, flowNode.config.approvalList)
                            }
                        }
                        this.nextNodeId = maxId
                    }
                    // 加载实例信息
                    if (["3", "4", "5"].includes(this.mode)) {
                        const request = {
                            instanceId: instanceId
                        }
                        http.post("/workflow/ru/instance", request).then(({ data }) => {
                            if (data.code === "0") {
                                this.instanceInfo = data.data
                                const formData = JSON.parse(data.data.formData)
                                for (let i = 0; i < this.formComponentList.length; i++) {
                                    this.formComponentList[i].componentValue = formData[this.formComponentList[i].componentCode]
                                }
                                for (let i = 0; i < this.flowNodeList.length; i++) {
                                    for (let j = 0; j < data.data.taskList.length; j++) {
                                        if (this.flowNodeList[i].nodeId === data.data.taskList[j].nodeId &&
                                            data.data.taskList[j].isDeleted !== "1") {
                                            this.flowNodeList[i].taskStatus = data.data.taskList[j].taskStatus
                                        }
                                    }
                                }
                                for (let i = 0; i < data.data.taskList.length; i++) {
                                    const newTask: Task = {
                                        ...data.data.taskList[i],
                                        userTaskList: [],
                                        taskCommentList: []
                                    }
                                    for (let j = 0; j < data.data.userTaskList.length; j++) {
                                        if (data.data.userTaskList[j].taskId === data.data.taskList[i].taskId) {
                                            newTask.userTaskList.push(data.data.userTaskList[j])
                                        }
                                    }
                                    for (let j = 0; j < data.data.taskCommentList.length; j++) {
                                        if (data.data.taskCommentList[j].taskId === data.data.taskList[i].taskId) {
                                            newTask.taskCommentList.push(data.data.taskCommentList[j])
                                        }
                                    }
                                    this.taskList.push(newTask)
                                }
                            }
                        })
                    }
                }
            })
        },
        save() {
            const request = buildSaveOrPublishRequest(this.modelInfo, this.formProperties, this.formComponentList, this.flowNodeList)
            http.post("/workflow/re/model/maintain", request).then(({ data }) => {
                if (data.code === "0") {
                    window.$notification['success']({
                        content: `保存成功`,
                        meta: `${data.msg}`,
                        duration: 2500,
                        keepAliveOnHover: true
                    })
                }
            })
        },
        publish() {
            const request = buildSaveOrPublishRequest(this.modelInfo, this.formProperties, this.formComponentList, this.flowNodeList)
            http.post("/workflow/re/model/publish", request).then(({ data }) => {
                if (data.code === "0") {
                    window.$notification['success']({
                        content: `发布成功`,
                        meta: `${data.msg}`,
                        duration: 2500,
                        keepAliveOnHover: true
                    })
                }
            })
        },
        formSubmit() {
            // 2：流程初始提交
            if (this.mode === "2") {
                const data: any = {}
                for (let i = 0; i < this.formComponentList.length; i++) {
                    data[this.formComponentList[i].componentCode] = this.formComponentList[i].componentValue
                }
                const request = {
                    modelId: this.modelInfo.modelId,
                    modelVersion: this.modelInfo.modelVersion,
                    data: data
                }
                http.post("/workflow/ru/instance/start", request).then(({ data }) => {
                    if (data.code === "0") {
                        window.$notification['success']({
                            content: `提交成功`,
                            meta: `${data.msg}`,
                            duration: 2500,
                            keepAliveOnHover: true
                        })
                    }
                })
            }
            // 5：运行时处理（用户重新提交任务）
            if (this.mode === "5") {
                const request = {
                    instanceId: this.instanceInfo?.instanceId
                }
                http.post("/workflow/ru/instance/temp-submit-start", request).then(({ data }) => {
                    if (data.code === "0") {
                        window.$notification['success']({
                            content: `提交成功`,
                            meta: `${data.msg}`,
                            duration: 2500,
                            keepAliveOnHover: true
                        })
                    }
                })
            }
        },
        tempSubmit() {
            // 2：流程初始提交
            if (this.mode === "2") {
                const data: any = {}
                for (let i = 0; i < this.formComponentList.length; i++) {
                    data[this.formComponentList[i].componentCode] = this.formComponentList[i].componentValue
                }
                const request = {
                    modelId: this.modelInfo.modelId,
                    modelVersion: this.modelInfo.modelVersion,
                    data: data
                }
                http.post("/workflow/ru/instance/temp-submit", request).then(({ data }) => {
                    if (data.code === "0") {
                        window.$notification['success']({
                            content: `提交成功`,
                            meta: `${data.msg}`,
                            duration: 2500,
                            keepAliveOnHover: true
                        })
                    }
                })
            }
            // 5：运行时处理（用户重新提交任务）
            if (this.mode === "5") {
                const data: any = {}
                for (let i = 0; i < this.formComponentList.length; i++) {
                    data[this.formComponentList[i].componentCode] = this.formComponentList[i].componentValue
                }
                const request = {
                    instanceId: this.instanceInfo?.instanceId,
                    data: data
                }
                http.post("/workflow/ru/instance/temp-submit-maintain", request).then(({ data }) => {
                    if (data.code === "0") {
                        window.$notification['success']({
                            content: `提交成功`,
                            meta: `${data.msg}`,
                            duration: 2500,
                            keepAliveOnHover: true
                        })
                    }
                })
            }
        }
    }
})

function getChildren(nodeList: DesignFlowNode[], nodeId: number): DesignFlowNode[] {
    const children: DesignFlowNode[] = []
    for (let i = 0; i < nodeList.length; i++) {
        if (nodeList[i].parentNodeId === nodeId) {
            if (nodeList[i].nodeType === "3" || nodeList[i].nodeType === "5") {
                continue
            } else if (nodeList[i].nodeType === "2" || nodeList[i].nodeType === "4") {
                const node = { ...nodeList[i] }
                node.branchNodeList = getBranchNodeList(nodeList, nodeList[i].nodeId)
                node.children = getChildren(nodeList, nodeList[i].nodeId)
                children.push(node)
            } else {
                const node = { ...nodeList[i] }
                node.children = getChildren(nodeList, nodeList[i].nodeId)
                children.push(node)
            }
        }
    }
    return children
}

function getBranchNodeList(nodeList: DesignFlowNode[], nodeId: number): DesignFlowNode[] {
    const branchNodeList: DesignFlowNode[] = []
    for (let i = 0; i < nodeList.length; i++) {
        if (nodeList[i].parentNodeId === nodeId && (nodeList[i].nodeType === "3" || nodeList[i].nodeType === "5")) {
            const node = { ...nodeList[i] }
            node.children = getChildren(nodeList, nodeList[i].nodeId)
            branchNodeList.push(node)
        }
    }
    return branchNodeList
}

function getNodeName(nodeType: string): string {
    if (nodeType === "1") {
        return "审批节点"
    }
    if (nodeType === "2") {
        return "条件节点"
    }
    if (nodeType === "3") {
        return "条件"
    }
    if (nodeType === "4") {
        return "并行节点"
    }
    if (nodeType === "5") {
        return "并行"
    }
    if (nodeType === "6") {
        return "触发器"
    }
    return "节点"
}

function getNodeCount(nodeList: DesignFlowNode[], nodeType: string): number {
    let count = 0
    for (let i = 0; i < nodeList.length; i++) {
        if (nodeList[i].nodeType === nodeType) {
            count++
        }
    }
    return count
}

function buildSaveOrPublishRequest(modelInfo: ModelInfo, formProperties: FormProperties, formComponentList: DesignFormComponent[], flowNodeList: DesignFlowNode[]) {
    const modelId = modelInfo.modelId
    const modelVersion = modelInfo.modelVersion
    const request: any = {
        model: { ...modelInfo },
        form: {
            modelId: modelId,
            modelVersion: modelVersion,
            ...formProperties
        },
        formComponentList: [],
        nodeList: [],
        propertyList: []
    }
    for (let i = 0; i < formComponentList.length; i++) {
        for (let key in formComponentList[i]) {
            const property = {
                modelId: modelId,
                modelVersion: modelVersion,
                objectId: formComponentList[i].componentCode,
                propertyKey: key,
                propertyValue: ""
            }

            if (formComponentList[i].componentType === "Divider") {
                if (["componentName", "componentType", "componentSpan", "componentTitlePlacement", "componentLineDashed"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }

            if (formComponentList[i].componentType === "Input") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "TextArea") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "Number") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "Radio") {
                if (["componentName", "componentType", "componentSpan", "componentRequired"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
                if (["componentOptions"].includes(key)) {
                    property.propertyValue = JSON.stringify(formComponentList[i].componentOptions)
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "CheckBox") {
                if (["componentName", "componentType", "componentSpan", "componentRequired"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
                if (["componentOptions"].includes(key)) {
                    property.propertyValue = JSON.stringify(formComponentList[i].componentOptions)
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "Select") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
                if (["componentOptions"].includes(key)) {
                    property.propertyValue = JSON.stringify(formComponentList[i].componentOptions)
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "MultiSelect") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
                if (["componentOptions"].includes(key)) {
                    property.propertyValue = JSON.stringify(formComponentList[i].componentOptions)
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "Date") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "DateRange") {
                if (["componentName", "componentType", "componentSpan", "componentRequired"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "DateTime") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "DateTimeRange") {
                if (["componentName", "componentType", "componentSpan", "componentRequired"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
            if (formComponentList[i].componentType === "Time") {
                if (["componentName", "componentType", "componentSpan", "componentRequired", "componentPlaceholder"].includes(key)) {
                    property.propertyValue = formComponentList[i][key as keyof DesignFormComponent] as string
                    request.propertyList.push(property)
                }
            }
        }
        request.formComponentList.push({
            modelId: modelId,
            modelVersion: modelVersion,
            ...formComponentList[i],
            componentSort: i
        })
    }
    for (let i = 0; i < flowNodeList.length; i++) {
        for (let key in flowNodeList[i].config) {
            const property = {
                modelId: modelId,
                modelVersion: modelVersion,
                objectId: flowNodeList[i].nodeId,
                propertyKey: key,
                propertyValue: ""
            }
            // 开始节点
            if (flowNodeList[i].nodeType === "0") {
                break
            }
            // 审批节点
            if (flowNodeList[i].nodeType === "1") {
                if (["approvalType", "approvalEnd", "multiApprovalType", "approvalRefusedType", "approvalRefusedToNodeId"].includes(key)) {
                    property.propertyValue = flowNodeList[i].config[key as keyof FlowNodeConfig] as string
                    request.propertyList.push(property)
                }
                if (["approvalList"].includes(key)) {
                    property.propertyValue = JSON.stringify(flowNodeList[i].config.approvalList)
                    request.propertyList.push(property)
                }
            }
            // 条件节点
            if (flowNodeList[i].nodeType === "2") {
                break
            }
            // 条件分支节点
            if (flowNodeList[i].nodeType === "3") {
                if (["conditionExpression"].includes(key)) {
                    property.propertyValue = flowNodeList[i].config[key as keyof FlowNodeConfig] as string
                    request.propertyList.push(property)
                }
            }
            // 并行节点
            if (flowNodeList[i].nodeType === "4") {
                break
            }
            // 并行分支节点
            if (flowNodeList[i].nodeType === "5") {
                break
            }
            // 触发器节点
            if (flowNodeList[i].nodeType === "6") {
                if (["triggerTarget"].includes(key)) {
                    property.propertyValue = flowNodeList[i].config[key as keyof FlowNodeConfig] as string
                    request.propertyList.push(property)
                }
            }
            // 结束节点
            if (flowNodeList[i].nodeType === "9") {
                break
            }
        }
        request.nodeList.push({
            modelId: modelId,
            modelVersion: modelVersion,
            ...flowNodeList[i]
        })
    }
    return request
}


export function getTaskStatusClass(mode: string, taskStatus?: string): string {
    if (["3", "4", "5"].includes(mode)) {
        if (taskStatus === "0") {
            return "primary"
        }
        if (taskStatus === "1") {
            return "info"
        }
        if (taskStatus === "2") {
            return "warning"
        }
        if (taskStatus === "3") {
            return "error"
        }
        if (taskStatus === "8") {
            return "error"
        }
        if (taskStatus === "9") {
            return "success"
        }
    }
    return ""
}